import time
import asyncio

async def fact(name, num):
    f = 1
    for i in range(2, num + 1):
        print(f"Task {name}: Compute factorial({num}), currently i={i}...")
        await asyncio.sleep(1)
        f *= i
    print(f"Task {name}: factorial({num}) = {f}")
    return f


async def main():
    await asyncio.gather(fact("A", 2), fact("B", 3), fact("C", 4))
    
    # gather 并行运行
    # task_a = asyncio.create_task(fact("A",2))
    # task_b = asyncio.create_task(fact("B",3))
    # task_c = asyncio.create_task(fact("C",4))
    # await task_a
    # await task_b
    # await task_c

print("start time: " + str(time.time()))
asyncio.run(main())
print("end time: " + str(time.time()))
